﻿using System;

namespace UniRx.Operators
{
	// Token: 0x020002E1 RID: 737
	internal class SampleObservable<T, T2> : OperatorObservableBase<T>
	{
		// Token: 0x06000FB6 RID: 4022 RVA: 0x00048C08 File Offset: 0x00047008
		public SampleObservable(IObservable<T> source, IObservable<T2> intervalSource) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.intervalSource = intervalSource;
		}

		// Token: 0x06000FB7 RID: 4023 RVA: 0x00048C24 File Offset: 0x00047024
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			return new SampleObservable<T, T2>.Sample(this, observer, cancel).Run();
		}

		// Token: 0x04000924 RID: 2340
		private readonly IObservable<T> source;

		// Token: 0x04000925 RID: 2341
		private readonly IObservable<T2> intervalSource;

		// Token: 0x020002E2 RID: 738
		private class Sample : OperatorObserverBase<T, T>
		{
			// Token: 0x06000FB8 RID: 4024 RVA: 0x00048C34 File Offset: 0x00047034
			public Sample(SampleObservable<T, T2> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000FB9 RID: 4025 RVA: 0x00048C6C File Offset: 0x0004706C
			public IDisposable Run()
			{
				this.sourceSubscription = new SingleAssignmentDisposable();
				this.sourceSubscription.Disposable = this.parent.source.Subscribe(this);
				IDisposable disposable = this.parent.intervalSource.Subscribe(new SampleObservable<T, T2>.Sample.SampleTick(this));
				return StableCompositeDisposable.Create(this.sourceSubscription, disposable);
			}

			// Token: 0x06000FBA RID: 4026 RVA: 0x00048CC4 File Offset: 0x000470C4
			public override void OnNext(T value)
			{
				object obj = this.gate;
				lock (obj)
				{
					this.latestValue = value;
					this.isUpdated = true;
				}
			}

			// Token: 0x06000FBB RID: 4027 RVA: 0x00048D08 File Offset: 0x00047108
			public override void OnError(Exception error)
			{
				object obj = this.gate;
				lock (obj)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x06000FBC RID: 4028 RVA: 0x00048D64 File Offset: 0x00047164
			public override void OnCompleted()
			{
				object obj = this.gate;
				lock (obj)
				{
					this.isCompleted = true;
					this.sourceSubscription.Dispose();
				}
			}

			// Token: 0x04000926 RID: 2342
			private readonly SampleObservable<T, T2> parent;

			// Token: 0x04000927 RID: 2343
			private readonly object gate = new object();

			// Token: 0x04000928 RID: 2344
			private T latestValue = default(T);

			// Token: 0x04000929 RID: 2345
			private bool isUpdated;

			// Token: 0x0400092A RID: 2346
			private bool isCompleted;

			// Token: 0x0400092B RID: 2347
			private SingleAssignmentDisposable sourceSubscription;

			// Token: 0x020002E3 RID: 739
			private class SampleTick : IObserver<T2>
			{
				// Token: 0x06000FBD RID: 4029 RVA: 0x00048DAC File Offset: 0x000471AC
				public SampleTick(SampleObservable<T, T2>.Sample parent)
				{
					this.parent = parent;
				}

				// Token: 0x06000FBE RID: 4030 RVA: 0x00048DBB File Offset: 0x000471BB
				public void OnCompleted()
				{
				}

				// Token: 0x06000FBF RID: 4031 RVA: 0x00048DBD File Offset: 0x000471BD
				public void OnError(Exception error)
				{
				}

				// Token: 0x06000FC0 RID: 4032 RVA: 0x00048DC0 File Offset: 0x000471C0
				public void OnNext(T2 _)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						if (this.parent.isUpdated)
						{
							T latestValue = this.parent.latestValue;
							this.parent.isUpdated = false;
							this.parent.observer.OnNext(latestValue);
						}
						if (this.parent.isCompleted)
						{
							try
							{
								this.parent.observer.OnCompleted();
							}
							finally
							{
								this.parent.Dispose();
							}
						}
					}
				}

				// Token: 0x0400092C RID: 2348
				private readonly SampleObservable<T, T2>.Sample parent;
			}
		}
	}
}
